//
//  LDSAAViewController.m
//  TestCategory
//
//  Created by kai_leedarson on 2017/10/17.
//Copyright © 2017年 maple_leedarson. All rights reserved.
//

#import "LDSAAViewController.h"
#import "LDSAAViewControllerViewModel.h"
#import "LDSCollectionViewModel.h"
@interface LDSAAViewController ()


@property(nonatomic, strong)  LDSAAViewControllerViewModel *viewControllerViewModel;//必须子类化
@property(nonatomic, strong) UICollectionView *collectionView;
@property(nonatomic, strong) LDSCollectionViewModel *collectionViewModel;//选择性子类化
@property(nonatomic, strong) UICollectionViewFlowLayout *collectionLayout;
@property(nonatomic, strong) NSMutableArray *dataSource;//cell 数据源
@end

@implementation LDSAAViewController

#pragma mark - lazy init
- (LDSAAViewControllerViewModel *)viewControllerViewModel {
    if (_viewControllerViewModel== nil) {
        _viewControllerViewModel = [[LDSAAViewControllerViewModel alloc] init];
        _viewControllerViewModel.delegate = (id<LDSViewControllerViewModelDelegate>) self;
    }
    return _viewControllerViewModel;
}

- (LDSCollectionViewModel *)collectionViewModel {
    if (_collectionViewModel == nil) {
        _collectionViewModel = [[LDSCollectionViewModel alloc] init];
        _collectionViewModel.delegate = (id<LDSCollectionViewModelDelegate>)self;
        //接管collectionView
        [_collectionViewModel handleWithCollection:self.collectionView];
        
    }
    return _collectionViewModel;
}

- (UICollectionView *)collectionView {
    if (_collectionView == nil) {
        _collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)
                                             collectionViewLayout:self.collectionLayout];
        //_collectionView.backgroundColor = RGB(235, 241, 241) ;
        [self.view addSubview:_collectionView];
    }
    return _collectionView;
}

- (UICollectionViewLayout *)collectionLayout {
    if (_collectionLayout == nil) {
        _collectionLayout = [[UICollectionViewFlowLayout alloc] init];
        [_collectionViewModel handleWithCollectionLayout:_collectionLayout];
        CGFloat photoSizeW = [UIScreen mainScreen].bounds.size.width/4;
        CGFloat photoSizeH = [UIScreen mainScreen].bounds.size.width/4-5*([UIScreen mainScreen].bounds.size.width)/768+25;
        //[_collectionLayout setMinimumInteritemSpacing:([[UIScreen mainScreen] bounds].size.width - 375) > 0 ? 8.0 : 6.0];
        //[_collectionLayout setMinimumLineSpacing:16.0];
        [_collectionLayout setItemSize:(CGSize){photoSizeW, photoSizeH}];
        [_collectionLayout setHeaderReferenceSize:(CGSize){[[UIScreen mainScreen] bounds].size.width, 23.0f}];
    }
    return _collectionLayout;
}

- (NSMutableArray *)dataSource {
    if (_dataSource == nil) {
        _dataSource = [NSMutableArray array];
    }
    return _dataSource;
}

- (NSString *)barItemTitle {
    return @"";
}

#pragma mark - life cycle
- (void)viewDidLoad {
    [super viewDidLoad];
    [self.viewControllerViewModel loadFetchData];
}

#pragma mark - LDSCollectionViewModelDelegate
- (void)lds_collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    
}

//- (UIEdgeInsets)lds_collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section {
//    return UIEdgeInsetsMake(14, 14,14, 14);
//}

#pragma mark - SecondVCViewModelDelegate
- (void)lds_didReceiveDataWithDataSource:(NSArray *)dataSource andSectionDataSource:(NSArray *)sectionDataSource {
    self.dataSource = [dataSource mutableCopy];
    [self.collectionViewModel handleWithDataSource:self.dataSource];
    [self.collectionView reloadData];
}
@end
